package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.DailyCommend;
import cn.wolfcode.trip.base.query.DailyCommendQueryObject;
import cn.wolfcode.trip.base.service.IDailyCommendService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("dailyCommend")
public class DailyCommendController {


    @Autowired
    private IDailyCommendService dailyCommendService;

    //推荐日报页面显示
    @RequestMapping("list")
    public String list(Model model , @ModelAttribute("qo") DailyCommendQueryObject qo){
        //按照推荐时间的降序和排序的优先度的升序排序
        qo.setOrderBy("dc.schedule desc , dc.sequence");
        PageInfo result = dailyCommendService.query(qo);
        model.addAttribute("result",result);
        return "dailyCommend/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(DailyCommend dailyCommend, MultipartFile file){
        JSONResult result = new JSONResult();
        try {
            if(file != null && file.getSize() > 0){
                // 将图片上传到图片服务器
                String imgUrl = UploadUtil.upload(file, UploadUtil.BASE_PATH + "upload");
                dailyCommend.setCoverUrl(imgUrl);
            }
            dailyCommendService.saveOrUpdate(dailyCommend);

        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("getContentByDailyCommendId")
    @ResponseBody
    public Object getContentByDailyCommendId(Long daliyCommendId){
        return  dailyCommendService.getContentByDailyCommendId(daliyCommendId);
    }





}
